package com.naiterui.ehp.bp.utils.notice;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.biz.AppJumpProtocol;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;

/**
 * 向app用户发送push通知(应用外)
 *
 * @author chenlin
 */
@SuppressWarnings("unused")
public class AppPushUtil {

    /**
     * 日志对象：<code>LOGGER</code>
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(AppPushUtil.class);
    /**
     * push发送用户类型--医生
     */
    private static final String USER_TYPE_DOCTOR = "d";
    /**
     * push发送用户类型--患者
     */
    private static final String USER_TYPE_PATIENT = "p";
    /**
     * push发送用户类型--药师
     */
    private static final String USER_TYPE_PHARMACIST = "ph";

    /**
     * 向患者发送push消息,当跳转协议为null时,默认发送到app首页。
     * 当跳转协议需要传递参数时,params为空时,会提示参数错误。
     */
    public static void sendPtPush(String url, Long userId, String message, AppJumpProtocol protocol, String... params) throws BusinessException {
        if (protocol == null) {
            protocol = AppJumpProtocol.PT_HOME;
        }
        AppPushUtil.sendPush(url, userId, USER_TYPE_PATIENT, message, protocol, params);
    }

    /**
     * 向医生发送push消息,当跳转协议为null时,默认发送到app首页。
     * 当跳转协议需要传递参数时,params为空时,会提示参数错误。
     */
    public static void sendDrPush(String url, Long userId, String message, AppJumpProtocol protocol, String... params) throws BusinessException {
        if (protocol == null) {
            protocol = AppJumpProtocol.DR_HOME;
        }
        AppPushUtil.sendPush(url, userId, USER_TYPE_DOCTOR, message, protocol, params);
    }

    /**
     * 向药师发送push消息,当跳转协议为null时,默认发送到app首页。
     * 当跳转协议需要传递参数时,params为空时,会提示参数错误。
     */
    public static void sendPhPush(String url, Long userId, String message, AppJumpProtocol protocol, String... params) throws BusinessException {
        if (protocol == null) {
            protocol = AppJumpProtocol.PH_HOME;
        }
        AppPushUtil.sendPush(url, userId, USER_TYPE_PHARMACIST, message, protocol, params);
    }

    private static void sendPush(String url, Long userId, String userType, String message, AppJumpProtocol protocol, String... params)
            throws BusinessException {
        if (protocol.getV() != null && params == null) {
            LOGGER.error("send push remote params is error 请求参数：url = {}, userId = {}, userType = {}, message = {}, protocol = {}, params = {}", url,
                         userId, userType, message, protocol, params);
            throw new BusinessException(BaseExceptionCodes.PARAM_ERROR);
        }
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("userId", String.valueOf(userId));
        requestParams.put("pushMessage", message);
        requestParams.put("userType", userType);
        requestParams.put("linkUrl", protocol.parse(params));

        RestHttpUtil.post().url(url).paramMap(requestParams).executeData();
    }

}
